package org.lsj.kit.service;

import org.lsj.kit.model.Perm;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 豆腐干Sama on 2019/3/4.
 */
@Service
public class MenuService {
    public List<Map<String,Object>> permToTree(Integer roleId, boolean hasResource, List<Perm> permList) {
        List<Map<String,Object>> treeList=new ArrayList<>();
        permList.forEach(perm -> {
            Map<String,Object> map=new HashMap<>();
            map.put("text",perm.getName());
            map.put("id",perm.getId());
            map.put("type","perm");
            map.put("name",perm.getName());
            map.put("url",perm.getUrl());
            map.put("pid",perm.getPid());

            if (perm.getPid() == null || perm.getPid().equals(0)) {
                map.put("state","open");
                treeList.add(map);
            } else {
                if (hasResource) {

                }
                for(Map<String,Object> tree:treeList){
                    if(perm.getPid()==Integer.parseInt(tree.get("id").toString())){
                        List<Map<String,Object>> childrenList= (List<Map<String, Object>>) tree.get("children");
                        if(childrenList==null){
                            childrenList=new ArrayList<>();
                            tree.put("children",childrenList);
                        }
                        childrenList.add(map);
                    }
                }
            }

        });
        return treeList;
    }
}
